﻿using System;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;


namespace CC
{
    /// <summary>
    /// 数据库操作的封装
    /// -designed by Hom
    /// </summary>

    public class CommonDataClass
    {
        public static string dsPath = "E:\\testDB.mdb";
        public static OleDbConnection conn = new OleDbConnection("provider=Microsoft.jet.OLEDB.4.0;Data Source="+dsPath);

        public static OleDbDataReader GetDataReader(string strSql)
        {
            OleDbDataReader OleDbdr = null;
            string cmdText = strSql;
            OleDbCommand cmd = new OleDbCommand(cmdText, conn);
            if (cmd.Connection.State.ToString() == "Closed")
            {
                cmd.Connection.Open();
            }
            try
            {
                OleDbdr = cmd.ExecuteReader();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            return OleDbdr;
        }

        public static bool CheckLogin(string username, string password)
        {
            bool islogin = false;
            OleDbDataReader OleDbdr = null;
            OleDbCommand cmd = new OleDbCommand("select * from [UserInfo] where userName='" + username + "'and pass  = '" + password + "'", conn);
            if (cmd.Connection.State.ToString() == "Closed")
            {
                cmd.Connection.Open();
            }
            try
            {
                OleDbdr = cmd.ExecuteReader();
                OleDbdr.Read();
                if (OleDbdr["userName"].ToString() == username)
                {
                    islogin = true;
                }
                else
                {
                    islogin = false;
                }
            }
            catch
            {
                islogin = false;
            }
            cmd.Connection.Close();
            return islogin;
        }

        public static bool UseQueryCommand(string strSql)
        {
            bool isSuccess = false;
            if (strSql.Trim() != "")
            {
                OleDbCommand cmd = new OleDbCommand(strSql.Trim(), conn);
                if (cmd.Connection.State.ToString() == "Closed")
                {
                    cmd.Connection.Open();
                }
                try
                {
                    cmd.ExecuteNonQuery();
                    isSuccess = true;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                    isSuccess = false;
                }
                cmd.Connection.Close();
            }
            return isSuccess;
        }

        public static DataSet GetQueryDataSet(string strSql, string strTable)
        {
            DataSet ds = new DataSet();
            if (strSql.Trim() != "")
            {
                try
                {
                    new OleDbDataAdapter(strSql, conn).Fill(ds, strTable);
                }
                catch
                {
                    ds = null;
                }
            }
            return ds;
        }

        public static void RefreshDataGrid(string strSql, DataGrid dataGrid, string strTable)
        {
            DataSet ds = GetQueryDataSet(strSql, strTable);
            dataGrid.DataSource = ds.Tables[strTable].DefaultView;
            dataGrid.Refresh();
        }

        public static string CheckUpdateDataSet(string strSql, DataSet changedataset)
        {
            string error = "";
            DataSet ds = new DataSet();
            if (strSql.Trim() != "")
            {
                try
                {
                    new OleDbDataAdapter(strSql, conn).Update(changedataset);
                    changedataset.AcceptChanges();
                    error = "ok";
                }
                catch (Exception e)
                {
                    if (e != null)
                    {
                        error = e.ToString();
                    }
                }
            }
            return error;
        }
    }
}
